﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Mirkuz.PowerManagement
{
    /// <summary>
    ///     D0  Full On. This is the state in which the device is on and running. It is receiving full power from the system and is delivering full functionality to the user.
    ///     D1  Low On. This is the state in which the device is fully functional at a lower power state than D0, a lower performance state than D0, or both. This state is applicable when the device is being used, but where peak performance is unnecessary and power is at a premium.
    ///     D2  Standby. This is the state in which the device is partially powered with automatic wakeup on request. The device is effectively standing by
    ///     D3  Sleep. This is the state in which the device is partially powered with device-initiated wakeup if available. A device in state D3 is sleeping but capable of raising the system power state on its own. It consumes only enough power to be able to do so; which must be less than or equal to the amount of power used in state D2.
    ///     D4  Off. This is the state in which the device is not powered. A device in state D4 should not be consuming any significant power. Some peripheral busses require static terminations that intrinsically use non-zero power when a device is physically connected to the bus; a device on such a bus can still support D4.
    /// </summary>
    public enum CEDEVICE_POWER_STATE : int {  PwrDeviceUnspecified = -1, D0 = 0, D1, D2, D3, D4, PwrDeviceMaximum };

    /// <summary>
    /// chiamate alle API di pm
    /// </summary>
    internal static class PMAPI
    {
        [DllImport("coredll.dll")]
        internal static extern UInt32 DevicePowerNotify(String pvDevice, int DeviceState, UInt32 Flags);

        [DllImport("coredll.dll")]
        internal static extern UInt32 SetDevicePower(String pvDevice, UInt32 dwDeviceFlags, int DeviceState);

        [DllImport("coredll.dll")]
        internal static extern bool CeRunAppAtTime(string pwszAppName, ref SystemTime lpTime);

        [DllImport("coredll.dll")]
        internal extern static UInt32 SetSystemPowerState(String pwsSystemState, UInt32 StateFlags, UInt32 Options);

        [DllImport("CoreDll.DLL")]
        internal static extern IntPtr SetPowerRequirement(String pvDevice, int DeviceState, UInt32 DeviceFlags, IntPtr pvSystemState, int StateFlags);

        [DllImport("coredll.dll")]
        internal extern static UInt32 ReleasePowerRequirement(IntPtr hPowerReq);

        [DllImport("coredll.dll")]
        internal static extern IntPtr RequestPowerNotifications(IntPtr hMsgQ, UInt32 Flags);

        [DllImport("coredll.dll")]
        internal static extern UInt32 WaitForSingleObject(IntPtr hHandle, int wait);

        [DllImport("coredll.dll")]
        internal static extern IntPtr CreateMsgQueue(string name, ref MsgQOptions options);

        [DllImport("coredll.dll")]
        internal static extern bool ReadMsgQueue(IntPtr hMsgQ, byte[] lpBuffer, UInt32 cbBufSize, ref UInt32 lpNumRead, int dwTimeout, ref UInt32 pdwFlags);

        [DllImport("CoreDll.dll")]
        internal static extern bool PowerPolicyNotify(uint dwMessage, uint onOrOff);

        [DllImport("coredll.dll")]
        internal static extern void SystemIdleTimerReset();

        internal static readonly UInt32 INFINITE = UInt32.MaxValue;
        internal static readonly UInt32 POWER_FORCE = 0x00001000;
        internal static readonly UInt32 POWER_NAME = 0x00000001;       
    }

    [Flags]
    internal enum PowerEventType : uint
    {
        PBT_TRANSITION = 0x00000001,
        PBT_RESUME = 0x00000002,
        PBT_POWERSTATUSCHANGE = 0x00000004,
        PBT_POWERINFOCHANGE = 0x00000008,
    }

    [Flags]
    internal enum PowerState : uint
    {
        POWER_STATE_ON = (0x00010000),
        POWER_STATE_OFF = (0x00020000),
        POWER_STATE_CRITICAL = (0x00040000),
        POWER_STATE_BOOT = (0x00080000),
        POWER_STATE_IDLE = (0x00100000),
        POWER_STATE_SUSPEND = (0x00200000),
        POWER_STATE_RESET = (0x00800000),
    }

    [Flags]
    internal enum PowerPolicyNotifyMessages : uint
    {
        PPN_REEVALUATESTATE = 0x0001,
        PPN_POWERCHANGE = 0x0002,
        PPN_UNATTENDEDMODE = 0x0003,
	    PPN_SUSPENDKEYPRESSED=0x0004,
        PPN_SUSPENDKEYRELEASED=0x0005,
        PPN_APPBUTTONPRESSED=0x0006
    }

}
